package androidlab.sprintingsquirrel;

import org.andengine.engine.Engine;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.ui.IGameInterface.OnCreateSceneCallback;
import androidlab.sprintingsquirrel.credits.CreditsScene;
import androidlab.sprintingsquirrel.editor.EditorScene;
import androidlab.sprintingsquirrel.game.GameScene;
import androidlab.sprintingsquirrel.menu.MainMenuScene;
import androidlab.sprintingsquirrel.sharing.SharingScene;
import androidlab.sprintingsquirrel.splash.SplashScene;

public class SceneManager {
	private static SceneManager manager = new SceneManager();
	
	private BaseScene splashScene;
    private BaseScene menuScene;
    private BaseScene gameScene;
	private BaseScene editorScene;
    private BaseScene creditsScene;
    private BaseScene sharingScene;
    private BaseScene loadingScene;
	
	private SceneType currentSceneType = SceneType.SPLASH;
	private BaseScene currentScene;
	
	private Engine engine = ResourceManager.getInstance().engine;

	public enum SceneType {
		SPLASH, MENU, GAME, EDITOR, CREDITS, SHARING, LOADING
	}
	
	public static SceneManager getInstance() {
		return manager;
	}
	
	public void createSplashScene(OnCreateSceneCallback pOnCreateSceneCallback) {
	    ResourceManager.getInstance().loadSplashScreen();
	    splashScene = new SplashScene();
	    currentScene = splashScene;
	    pOnCreateSceneCallback.onCreateSceneFinished(splashScene);
	}
	
	public void createMenuScene() {
		ResourceManager.getInstance().loadMenuResources();
	    menuScene = new MainMenuScene();
	    ResourceManager.getInstance().loadLoadingResources();
	    loadingScene = new LoadingScene();
	    setCurrentScene(SceneType.MENU);
	    disposeSplashScene();
	}
	
	public void createGameScene(final String level) {
		setCurrentScene(SceneType.LOADING);
		ResourceManager.getInstance().unloadMenuTextures();
		
		engine.registerUpdateHandler(new TimerHandler(3.0f, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				engine.unregisterUpdateHandler(pTimerHandler);
				ResourceManager.getInstance().loadGameResources(level);
		        gameScene = new GameScene();
				setCurrentScene(SceneType.GAME);
				ResourceManager.getInstance().unloadLoadingTextures();
			}
		}));
	}
	
	public void createEditorScene(final String level, final int x, final int y) {
		setCurrentScene(SceneType.LOADING);
		ResourceManager.getInstance().unloadMenuTextures();
		
		engine.registerUpdateHandler(new TimerHandler(2.0f, new ITimerCallback() {
			
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				engine.unregisterUpdateHandler(pTimerHandler);
				ResourceManager.getInstance().loadEditorResources(level, x, y);
				editorScene = new EditorScene();
				editorScene.setOnAreaTouchTraversalFrontToBack();
				setCurrentScene(SceneType.EDITOR);
				ResourceManager.getInstance().unloadLoadingTextures();
			}
		}));
	}
	
	public void createLoadingScene() {
		ResourceManager.getInstance().loadLoadingResources();
		loadingScene = new LoadingScene();
		setCurrentScene(SceneType.LOADING);
	}
	
	public void createCreditsScene() {
		creditsScene = new CreditsScene();
		setCurrentScene(SceneType.CREDITS);
	}
	
	public void createSharingScene() {
		ResourceManager.getInstance().loadSharingResources();
		sharingScene = new SharingScene();
		setCurrentScene(SceneType.SHARING);
		ResourceManager.getInstance().unloadMenuTextures();
	}
		
	public void loadMenuScene() {
        ResourceManager.getInstance().loadMenuTextures();
        ResourceManager.getInstance().loadLoadingTextures();
        if(getCurrentSceneType() == SceneType.GAME) {
        	setCurrentScene(SceneType.MENU);
        	gameScene.disposeScene();
        	ResourceManager.getInstance().unloadGameTextures();
        }
        else if(getCurrentSceneType() == SceneType.EDITOR) {
        	setCurrentScene(SceneType.MENU);
        	editorScene.disposeScene();
        	ResourceManager.getInstance().unloadEditorTextures();
        }
        else if(getCurrentSceneType() == SceneType.SHARING) {
        	setCurrentScene(SceneType.MENU);
        	sharingScene.disposeScene();
        	ResourceManager.getInstance().unloadSharingTextures();
        }
        else if(getCurrentSceneType() == SceneType.CREDITS) {
        	setCurrentScene(SceneType.MENU);
        	creditsScene.disposeScene();
        }
	}
	
	private void disposeSplashScene() {
	    ResourceManager.getInstance().unloadSplashScreen();
	    splashScene.disposeScene();
	    splashScene = null;
	}

	public SceneType getCurrentSceneType() {
		return currentSceneType;
	}
	
	public BaseScene getCurrentScene()	 {
        return currentScene;
    }

	public void setCurrentScene(SceneType currentSceneType) {
		this.currentSceneType = currentSceneType;
		
		switch (currentSceneType) {
		case SPLASH:
			engine.setScene(splashScene);
			currentScene = splashScene;
			break;
		case MENU:
			engine.setScene(menuScene);
			currentScene = menuScene;
			break;
		case GAME:
			engine.setScene(gameScene);
			currentScene = gameScene;
			break;
		case EDITOR:
			engine.setScene(editorScene);
			currentScene = editorScene;
			break;
		case CREDITS:
			engine.setScene(creditsScene);
			currentScene = creditsScene;
			break;
		case SHARING:
			engine.setScene(sharingScene);
			currentScene = sharingScene;
			break;
		case LOADING:
			engine.setScene(loadingScene);
			currentScene = loadingScene;
			break;
		default:
			break;
		}
	}
	
}
